<html>
  <head>
  <title>unpack.py</title>
  </head>
  <body>
  <h3>unpack.py (<a href="../unpack.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># unpack.py
# ---------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: blue; font-weight: bold">import </span>os<span style="font-weight: bold">, </span>cPickle<span style="font-weight: bold">, </span>sys

<span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span>sys<span style="font-weight: bold">.</span>argv<span style="font-weight: bold">) != </span><span style="color: red">3</span><span style="font-weight: bold">: 
  </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">'Usage: %s stats_file team_name' </span><span style="font-weight: bold">% </span>sys<span style="font-weight: bold">.</span>argv<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
  </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">'Unpacks the stats file of a server into a bunch of replay files.'
  </span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span>sys<span style="font-weight: bold">.</span>argv<span style="font-weight: bold">) == </span><span style="color: red">2</span><span style="font-weight: bold">: 
    </span>d <span style="font-weight: bold">= </span>cPickle<span style="font-weight: bold">.</span>load<span style="font-weight: bold">(</span>open<span style="font-weight: bold">(</span>sys<span style="font-weight: bold">.</span>argv<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]))
    </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">'Team names:'</span><span style="font-weight: bold">, </span>d<span style="font-weight: bold">.</span>keys<span style="font-weight: bold">()
  </span>sys<span style="font-weight: bold">.</span>exit<span style="font-weight: bold">(</span><span style="color: red">2</span><span style="font-weight: bold">)

</span>d <span style="font-weight: bold">= </span>cPickle<span style="font-weight: bold">.</span>load<span style="font-weight: bold">(</span>open<span style="font-weight: bold">(</span>sys<span style="font-weight: bold">.</span>argv<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">]))
</span>user <span style="font-weight: bold">= </span>sys<span style="font-weight: bold">.</span>argv<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">]
</span>k <span style="font-weight: bold">= </span><span style="color: red">0
</span><span style="color: blue; font-weight: bold">print </span><span style="color: red">'Unpacking games for'</span><span style="font-weight: bold">, </span>user
<span style="color: blue; font-weight: bold">for </span>g<span style="font-weight: bold">, </span>w <span style="color: blue; font-weight: bold">in </span>d<span style="font-weight: bold">[</span>user<span style="font-weight: bold">][</span><span style="color: red">'gameHistory'</span><span style="font-weight: bold">]:
    </span>k <span style="font-weight: bold">+= </span><span style="color: red">1
    </span>t <span style="font-weight: bold">= {</span><span style="color: red">'layout'</span><span style="font-weight: bold">: </span>g<span style="font-weight: bold">.</span>state<span style="font-weight: bold">.</span>data<span style="font-weight: bold">.</span>layout<span style="font-weight: bold">, </span><span style="color: red">'agents'</span><span style="font-weight: bold">: </span>g<span style="font-weight: bold">.</span>agents<span style="font-weight: bold">, </span><span style="color: red">'actions'</span><span style="font-weight: bold">: </span>g<span style="font-weight: bold">.</span>moveHistory<span style="font-weight: bold">, </span><span style="color: red">'length'</span><span style="font-weight: bold">: </span>g<span style="font-weight: bold">.</span>length<span style="font-weight: bold">}
    </span>fname <span style="font-weight: bold">= </span><span style="color: red">'replay_' </span><span style="font-weight: bold">+ </span>user <span style="font-weight: bold">+ </span><span style="color: red">'_' </span><span style="font-weight: bold">+ </span>str<span style="font-weight: bold">(</span>k<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">'Game:'</span><span style="font-weight: bold">, </span>fname
    cPickle<span style="font-weight: bold">.</span>dump<span style="font-weight: bold">(</span>t<span style="font-weight: bold">,</span>file<span style="font-weight: bold">(</span>fname<span style="font-weight: bold">, </span><span style="color: red">'w'</span><span style="font-weight: bold">))</span>
  </pre>
  </body>
  </html>
  